<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="班级名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入班级名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="queryParams.expireTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="班级来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入班级来源"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="班级码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入班级码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openAddFreeForm('create')"
          v-hasPermi="['exam:user-class:create']"
        >
          免费新增
        </el-button>
        <el-popover
          placement="top-start"
          title=""
          :width="200"
          trigger="hover"
          content="1. 商品选购->班级; 2. 我的班级->完善"
        >
          <template #reference>
            <el-button class="m-2"
                       type="primary"
                       @click="openAddOrderForm()">付费新增</el-button>
          </template>
        </el-popover>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="班级名称" align="center" prop="name" />
      <el-table-column label="持有者" align="center" prop="userId" />
      <el-table-column label="持有者名称" align="center" prop="teacher" />
      <el-table-column label="成员人数上限" align="center" prop="maxMemCount" />
      <el-table-column label="已加入成员个数" align="center" prop="memCount" />
      <el-table-column
        label="过期时间"
        align="center"
        prop="expireTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="班级来源" align="center" prop="source" />
      <el-table-column label="班级码" align="center" prop="code" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            v-if="scope.row.status === 'WAIT_USE'"
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['exam:user-class:update']"
          >
            完善
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['exam:user-class:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：完善 -->
  <TeacherClassForm ref="fillFormRef" @success="getList" />
  <!-- 表单弹窗：完善 -->
  <TeacherClassAddForm ref="addFreeFormRef" @success="getList" />
  <!-- 子表的列表 -->
  <ContentWrap>
    <el-tabs model-value="userClassRecord">
      <el-tab-pane label="班级-记录" name="userClassRecord">
        <UserClassRecordList :class-id="currentRow.id" :detail="true"/>
      </el-tab-pane>
      <el-tab-pane label="班级-成员" name="userClassMem">
        <UserClassMemList :class-id="currentRow.id" :detail="true"/>
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UserClassApi, UserClassVO } from '@/api/exam/userclass'
import UserClassMemList from '@/views/exam/components/class/UserClassMemList.vue'
import UserClassRecordList from '@/views/exam/components/class/UserClassRecordList.vue'
import TeacherClassForm from "@/views/exam/center/member/class/teacher/TeacherClassForm.vue";
import TeacherClassAddForm from "@/views/exam/center/member/class/teacher/TeacherClassAddForm.vue";
const { push } = useRouter() // 路由跳转
/** 用户-班级 列表 */
defineOptions({ name: 'MemberTeacherClass' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UserClassVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  userId: undefined,
  teacher: undefined,
  maxMemCount: undefined,
  memCount: undefined,
  expireTime: [],
  source: undefined,
  code: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserClassApi.getUserClassPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const fillFormRef = ref()
const openForm = (type: string, id?: number) => {
  fillFormRef.value.open(type, id)
}

/** 添加免费操作 */
const addFreeFormRef = ref()
const openAddFreeForm = (type: string, id?: number) => {
  addFreeFormRef.value.open(type)
}

/** 添加付费班级操作 */
const openAddOrderForm = () => {
  // 新增
  push({ name: 'MemberClassSpu' });
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserClassApi.deleteUserClass(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserClassApi.exportUserClass(queryParams)
    download.excel(data, '用户-班级.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
